package com.hongkzh.www.look.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hongkzh.www.R;
import com.hongkzh.www.look.model.bean.CityAdvBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by CJJ on 2017/3/7.
 * 乐看 城市 头部 adapter
 */

public class LkcsRvHorAdapter extends RecyclerView.Adapter<LkcsRvHorAdapter.ViewHolder> {
    private List<CityAdvBean.DataBean> data = new ArrayList<>();

    public LkcsRvHorAdapter(CityAdvBean cityAdvBean) {
        if (cityAdvBean != null) {
            List<CityAdvBean.DataBean> data = cityAdvBean.getData();
            for (int i = 0; i < data.size(); i++) {
                this.data.add(data.get(data.size() - 1 - i));
            }
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lkcsrvhor, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Glide.with(holder.itemView).load(data.get(position).getCoverImgSrc()).into(holder.lkcrIma);
        holder.lkcrTitle.setText(data.get(position).getTitle());
        holder.lkcrImgNote.setText(data.get(position).getImgNote());
        if (data.get(position).getTags().size() > 0) {
            holder.lkcrLabel.setVisibility(View.VISIBLE);
            holder.lkcrLabel.setText(data.get(position).getTags().get(0).getLabel());
            if (data.get(position).getTags().size() > 1) {
                holder.lkcrLabel1.setVisibility(View.VISIBLE);
                holder.lkcrLabel1.setText(data.get(position).getTags().get(1).getLabel());
                if (data.get(position).getTags().size() > 2) {
                    holder.lkcrLabel2.setVisibility(View.VISIBLE);
                    holder.lkcrLabel2.setText(data.get(position).getTags().get(2).getLabel());
                    if (data.get(position).getTags().size() > 3) {
                        holder.lkcrLabel3.setVisibility(View.VISIBLE);
                        holder.lkcrLabel3.setText(data.get(position).getTags().get(3).getLabel());
                    }
                }
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(data.get(position).getUid(), data.get(position).getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void add(CityAdvBean cityAdvBean) {
        if (cityAdvBean != null) {
            List<CityAdvBean.DataBean> data = cityAdvBean.getData();
            for (int i = 0; i < this.data.size(); i++) {
                this.data.add(data.get(this.data.size() - 1 - i));
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(String uid, String id);
    }

    private OnItemClickListener onItemClickListener;

    void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.lkcr_ima)
        ImageView lkcrIma;
        @BindView(R.id.lkcr_label)
        TextView lkcrLabel;
        @BindView(R.id.lkcr_label1)
        TextView lkcrLabel1;
        @BindView(R.id.lkcr_label2)
        TextView lkcrLabel2;
        @BindView(R.id.lkcr_label3)
        TextView lkcrLabel3;
        @BindView(R.id.lkcr_title)
        TextView lkcrTitle;
        @BindView(R.id.lkcr_imgNote)
        TextView lkcrImgNote;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
